package com.example.shopping2.a_c_fragment.classa

import BaseFragment
import IFragment
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.bumptech.glide.Glide
import com.example.shopping2.R
import com.example.shopping2.a_a_entity.BaseEntity
import com.example.shopping2.a_a_mvp.contract.HomeContract
import com.example.shopping2.a_a_mvp.model.models.HomeModel
import com.example.shopping2.a_a_mvp.presenter.presenters.HomePresenter
import com.example.shopping2.a_c_fragment.classa.adapter.Left_Adapter
import com.example.shopping2.a_c_fragment.classa.adapter.Right_Adapter
import com.example.shopping2.a_c_fragment.classa.entity.ClassRight_Entity
import com.example.shopping2.a_c_fragment.classa.entity.Classleft_Entity
import com.example.shopping2.a_c_network.HttpCode
import com.example.shopping2.a_c_network.LjlUtils

class ClassFragment : BaseFragment<HomePresenter>(), HomeContract.HomeView, IFragment {
    private val classLeftRec: RecyclerView by lazy { findViewById<RecyclerView>(R.id.class_left_rec) }
    private val classImg: ImageView by lazy { findViewById<ImageView>(R.id.class_img) }
    private val classRightRec: RecyclerView by lazy { findViewById<RecyclerView>(R.id.class_right_rec) }
    private var imglist = arrayListOf<String>(
        "https://i01piccdn.sogoucdn.com/082fb9a40bab4521",
        "https://i01piccdn.sogoucdn.com/0248ef0ef1d0faf9",
        "https://i02piccdn.sogoucdn.com/367da58fe13055a5",
        "https://i02piccdn.sogoucdn.com/367da58fe13055a5",
        "https://i02piccdn.sogoucdn.com/367da58fe13055a5",
        "https://i02piccdn.sogoucdn.com/367da58fe13055a5",
        "https://i02piccdn.sogoucdn.com/0de8dcfc26ef408f"
    )
    fun getimg(pistion:Int):String{
        return imglist[pistion%imglist.size]
    }
    override fun findlayout(): Int {
        return R.layout.fragment_class
    }

    override fun initView() {

    }

    override fun initData() {
        mypre = HomePresenter(HomeModel(), this)
        var list: ArrayList<HashMap<String, Any>> = ArrayList()
        var map: HashMap<String, Any> = HashMap()
        map.put("parentId", 0)
        map.put("code", HttpCode.CLASSLEFT)
        list.add(map)
        mypre.requestAll(list)
    }


    override fun showHomeData(baseEntity: BaseEntity) {
        if (baseEntity is Classleft_Entity) {
            var data = baseEntity.data
            var adapter = Left_Adapter(data as ArrayList<Classleft_Entity.DataBean>)
            classLeftRec.adapter = adapter
            classLeftRec.layoutManager = LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)

            //设置图片
            Glide.with(classImg).load(getimg(0)).into(classImg)
            //点击改变样式   右边跟着刷新
            //第一个默认选中 并请求一遍数据
            data[0].checked = true
            var list: ArrayList<HashMap<String, Any>> = ArrayList()
            var map: HashMap<String, Any> = HashMap()
            map.put("parentId", 1)
            map.put("code", HttpCode.CLASSRIGHT)
            list.add(map)
            mypre.requestAll(list)

            adapter.setOnItemClickListener { adapter, view, position ->
                Glide.with(classImg).load(getimg(position)).into(classImg)
                for (i in 0..data.size - 1) {
                    data[i].checked = false
                    if (i == position) {
                        data[i].checked = true
                        adapter.notifyDataSetChanged()
                    }
                }
                for (i in 0..data.size - 1) {
                    if (data[i].checked) {
                        var parentId = data[i].id
                        var list: ArrayList<HashMap<String, Any>> = ArrayList()
                        var map: HashMap<String, Any> = HashMap()
                        map.put("parentId", parentId)
                        map.put("code", HttpCode.CLASSRIGHT)
                        list.add(map)
                        mypre.requestAll(list)
                    }
                }
            }


        } else if (baseEntity is ClassRight_Entity) {
            var data = baseEntity.data
            if (data == null) {

            } else {
                var adapter = Right_Adapter(data as ArrayList<ClassRight_Entity.DataBean>)
                classRightRec.adapter = adapter
                classRightRec.layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
                //跳入详情页
                adapter.setOnItemChildClickListener { adapter, view, position ->

                }
            }


        }

    }


}